<template>
  <div>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item  label="栏目分类名称:" prop="typename" style="white-space: nowrap">
        <el-input v-model="ruleForm.typename"></el-input>
      </el-form-item>
      <el-form-item label="排序:" prop="sort" style="white-space: nowrap">
        <el-input v-model.number="ruleForm.sort" type="number"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">更新</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      center
      >
      <span style="width:100%;text-align: center">{{ msg }}</span>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'edit-news-type',
  data () {
    var checkSort = (rule, value, callback) => {
      if (Number.isInteger(value)) {
        if (value === 0) {
          return callback(new Error('排序不能为0'))
        }
      }

      if (!value) {
        return callback(new Error('排序不能为空'))
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error('请输入整数'))
        } else {
          if (value <= 0) {
            callback(new Error('必须大于0'))
          } else {
            callback()
          }
        }
      }, 1000)
    }
    return {
      ruleForm: {
        typename: this.$route.params.typename,
        sort: this.$route.params.sort,
        id: this.$route.params.id
      },
      dialogVisible: false,
      msg: ' ',
      rules: {
        typename: [
          { required: true, message: '请输入栏目分类名称', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 40 个字符', trigger: 'blur' }
        ],
        sort: [
          { validator: checkSort, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$http.get('/news/updateNewsType', {
            params: {
              typeName: this.ruleForm.typename,
              sort: this.ruleForm.sort,
              id: this.ruleForm.id
            }
          })
            .then(res => {
              let data = res.data
              let status = data.status
              if (status === 200) {
                this.dialogVisible = true
                this.ruleForm.typename = ''
                this.msg = '更新成功'
              } else {
                this.msg = '更新失败'
              }
            })
        } else {
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">

</style>
